package com.cup.bddm.impala.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.dao.BranchDao;
import com.cup.bddm.impala.po.BranchDailyTrans;
import com.cup.bddm.impala.po.BranchGeneDailyTrans;
import com.cup.bddm.impala.po.BranchStreetStatInfo;
import com.cup.bddm.impala.po.ServiceMchntStatInfo;
import com.cup.bddm.impala.service.BranchService;
import com.cup.bddm.impala.vo.BranchServiceMchntStatVo;
import com.cup.bddm.impala.vo.BranchStreetStatVo;
import com.cup.bddm.impala.vo.BranchTransDistVo;
import com.cup.bddm.impala.vo.BranchTransInfoVo;
import com.cup.bddm.impala.vo.CityGeneDailyTransVo;
import com.cup.bddm.impala.vo.TransDist;

@Service
public class BranchService {

	static Logger logger = Logger.getLogger(BranchService.class);

	@Autowired
	private BranchDao branchDao;

	public BranchTransInfoVo getBranchTransInfo(String branchCd, String date) {
		logger.info("get branch trans info detail,branchCd=" + branchCd + ",date=" + date);

		BranchTransInfoVo branchTransInfo = new BranchTransInfoVo();
		try {

			BranchDailyTrans dayTrans = branchDao.getBranchDailyTrans(branchCd, date, "all");
			branchTransInfo.setDay_trans(dayTrans);
			branchTransInfo.setState(Constant.SUCCESS);

		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			branchTransInfo.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			branchTransInfo.setState(Constant.INTER_ERROR);
		}
		return branchTransInfo;
	}

	public CityGeneDailyTransVo getCityGeneDailyTransInfo(String branchCd, String date) {
		logger.info("get branch trans info detail");
		CityGeneDailyTransVo cityGeneDailyTrans = new CityGeneDailyTransVo();

		try {
			List<BranchGeneDailyTrans> branchGeneTransList = branchDao.getCityGeneTrans(branchCd, date);
			BigDecimal totalSqCount = new BigDecimal("0");
			BigDecimal totalJqCount = new BigDecimal("0");
			BigDecimal totalNfcTransCount = new BigDecimal("0");
			BigDecimal totalQrcTransCount = new BigDecimal("0");
			BigDecimal totalOfflineTransCount = new BigDecimal("0");
			List<TransDist> branch_qrc_trans_count_list = new ArrayList<TransDist>();
			List<TransDist> branch_nfc_trans_count_list = new ArrayList<TransDist>();
			List<TransDist> branch_offline_trans_count_list = new ArrayList<TransDist>();
			List<TransDist> branchTransCountList = new ArrayList<TransDist>();
			List<TransDist> branchTransSumList = new ArrayList<TransDist>();
			List<TransDist> branchSqCountList = new ArrayList<TransDist>();
			List<TransDist> branchJqCountList = new ArrayList<TransDist>();
			List<TransDist> branchMchntCountList = new ArrayList<TransDist>();

			for (BranchGeneDailyTrans branchGeneTrans : branchGeneTransList) {

				// 累计商圈数，街区数，nfc交易笔数，二维码交易笔数
				totalSqCount = totalSqCount.add(branchGeneTrans.getSq_count());
				totalJqCount = totalJqCount.add(branchGeneTrans.getJq_count());
				totalNfcTransCount = totalNfcTransCount.add(branchGeneTrans.getNfc_trans_count());
				totalQrcTransCount = totalQrcTransCount.add(branchGeneTrans.getQrc_trans_count());
				totalOfflineTransCount = totalOfflineTransCount.add(branchGeneTrans.getOffline_trans_count());

				// 二维码交易笔数
				TransDist branchQrcTransCount = new TransDist();
				branchQrcTransCount.setName(branchGeneTrans.getCity());
				branchQrcTransCount.setValue(branchGeneTrans.getQrc_trans_count());
				branch_qrc_trans_count_list.add(branchQrcTransCount);
				// NFC交易笔数
				TransDist branchNfcTransCount = new TransDist();
				branchNfcTransCount.setName(branchGeneTrans.getCity());
				branchNfcTransCount.setValue(branchGeneTrans.getNfc_trans_count());
				branch_nfc_trans_count_list.add(branchNfcTransCount);
				// 线下交易笔数
				TransDist branchOfflineTransCount = new TransDist();
				branchOfflineTransCount.setName(branchGeneTrans.getCity());
				branchOfflineTransCount.setValue(branchGeneTrans.getOffline_trans_count());
				branch_offline_trans_count_list.add(branchOfflineTransCount);

				// 交易笔数列表
				TransDist branchTransCount = new TransDist();
				branchTransCount.setName(branchGeneTrans.getCity());
				branchTransCount.setValue(branchGeneTrans.getAll_trans_count());

				branchTransCountList.add(branchTransCount);

				// 交易金额列表
				TransDist branchTransSum = new TransDist();
				branchTransSum.setName(branchGeneTrans.getCity());
				branchTransSum.setValue(branchGeneTrans.getAll_trans_sum());

				branchTransSumList.add(branchTransSum);

				// 商圈数列表
				TransDist branchSqCount = new TransDist();
				branchSqCount.setName(branchGeneTrans.getCity());
				branchSqCount.setValue(branchGeneTrans.getSq_count());

				branchSqCountList.add(branchSqCount);

				// 街区数列表
				TransDist branchJqCount = new TransDist();
				branchJqCount.setName(branchGeneTrans.getCity());
				branchJqCount.setValue(branchGeneTrans.getJq_count());

				branchJqCountList.add(branchJqCount);

				// 商户数列表
				TransDist branchMchntCount = new TransDist();
				branchMchntCount.setName(branchGeneTrans.getCity());
				branchMchntCount.setValue(branchGeneTrans.getMchnt_count());

				branchMchntCountList.add(branchMchntCount);

			}

			cityGeneDailyTrans.setSq_count(totalSqCount);
			cityGeneDailyTrans.setJq_count(totalJqCount);
			cityGeneDailyTrans.setNfc_trans_count(totalNfcTransCount);
			cityGeneDailyTrans.setQrc_trans_count(totalQrcTransCount);
			cityGeneDailyTrans.setOffline_trans_count(totalOfflineTransCount);

			cityGeneDailyTrans.setBranch_qrc_trans_count_list(branch_qrc_trans_count_list);
			cityGeneDailyTrans.setBranch_nfc_trans_count_list(branch_nfc_trans_count_list);
			cityGeneDailyTrans.setBranch_offline_trans_count_list(branch_offline_trans_count_list);

			cityGeneDailyTrans.setBranch_sq_count_list(branchSqCountList);
			cityGeneDailyTrans.setBranch_jq_count_list(branchJqCountList);
			cityGeneDailyTrans.setBranch_mchnt_count_list(branchMchntCountList);
			cityGeneDailyTrans.setBranch_trans_count_list(branchTransCountList);
			cityGeneDailyTrans.setBranch_trans_sum_list(branchTransSumList);

			cityGeneDailyTrans.setNearestDate(date);
			cityGeneDailyTrans.setState(Constant.SUCCESS);
		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			cityGeneDailyTrans.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			cityGeneDailyTrans.setState(Constant.INTER_ERROR);
		}

		return cityGeneDailyTrans;
	}

	public BranchTransDistVo getBranchTransByTransType(String branchCd, String date) {
		logger.info("get branch trans distribute by trans type ,branchCd=" + branchCd + ",date=" + date);
		BranchTransDistVo branchTransDistVo = new BranchTransDistVo();

		try {

			// 获取nfc/qrc/offline的交易信息
			Map<String, BranchDailyTrans> transTypeMap = branchDao.getBranchTransByTransType(branchCd, date);
			List<TransDist> trans_dist = new ArrayList<TransDist>();
			for (Map.Entry<String, BranchDailyTrans> entry : transTypeMap.entrySet()) {
				TransDist trans = new TransDist();
				trans.setName(entry.getKey());
				trans.setValue(entry.getValue().getTrans_count());
				trans_dist.add(trans);
			}

			branchTransDistVo.setTrans_dist(trans_dist);
			branchTransDistVo.setState(Constant.SUCCESS);

		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			branchTransDistVo.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			branchTransDistVo.setState(Constant.INTER_ERROR);
		}

		return branchTransDistVo;
	}

	public BranchTransDistVo getBranchTransByMchntType(String branchCd, String date) {
		logger.info("get branch trans distribute by mchnt type ,branchCd=" + branchCd + ",date=" + date);
		BranchTransDistVo branchTransDistVo = new BranchTransDistVo();

		try {

			// 获取商户类型的交易信息
			Map<String, BranchDailyTrans> mchntTypeMap = branchDao.getBranchTransByMchntType(branchCd, date);
			List<TransDist> trans_dist = new ArrayList<TransDist>();
			for (Map.Entry<String, BranchDailyTrans> entry : mchntTypeMap.entrySet()) {
				TransDist trans = new TransDist();
				trans.setName(entry.getKey());
				trans.setValue(entry.getValue().getTrans_count());
				trans_dist.add(trans);
			}

			branchTransDistVo.setTrans_dist(trans_dist);
			branchTransDistVo.setState(Constant.SUCCESS);

		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			branchTransDistVo.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			branchTransDistVo.setState(Constant.INTER_ERROR);
		}

		return branchTransDistVo;
	}

	//
	// public BranchGeneDailyTransVo getBranchGeneDailyTransInfo(String
	// branchCd, String date) {
	// logger.info("get branch general trans info ,branchCd=" + branchCd +
	// ",date=" + date);
	// BranchGeneDailyTransVo branchGeneDailyTrans = new
	// BranchGeneDailyTransVo();
	//
	//
	// try {
	//
	// BranchGeneDailyTrans dayTrans =
	// branchDao.getBranchGeneralDailyTrans(branchCd, date);
	// branchGeneDailyTrans.setGeneral_trans_info(dayTrans);
	// branchGeneDailyTrans.setState(Constant.SUCCESS);
	//
	// } catch (RuntimeException e) {
	// logger.error("internal sql error!",e);
	// branchGeneDailyTrans.setState(Constant.INTER_ERROR);
	//
	// } catch (Exception e) {
	// logger.error("internal error!",e);
	// branchGeneDailyTrans.setState(Constant.INTER_ERROR);
	// }
	// return branchGeneDailyTrans;
	// }

	public BranchStreetStatVo getBranchStreetStatInfo(String branchCd, String date) {
		logger.info("get branch street stat info ,branchCd=" + branchCd + ",date=" + date);
		BranchStreetStatVo branchStreetStat = new BranchStreetStatVo();

		try {

			List<BranchStreetStatInfo> streetStatList = branchDao.getBranchStreetStatInfo(branchCd, date);
			branchStreetStat.setStreet_stat_list(streetStatList);
			branchStreetStat.setState(Constant.SUCCESS);

		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			branchStreetStat.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			branchStreetStat.setState(Constant.INTER_ERROR);
		}
		return branchStreetStat;
	}

	public BranchServiceMchntStatVo getBranchServiceMchntStatInfo(String branchCd, String date) {
		logger.info("get branch service merchant stat info ,branchCd=" + branchCd + ",date=" + date);
		BranchServiceMchntStatVo branchServiceMchntStat = new BranchServiceMchntStatVo();

		try {

			List<ServiceMchntStatInfo> serviceMchntStatList = branchDao.getBranchServiceMchntStatInfo(branchCd, date);
			branchServiceMchntStat.setService_mchnt_stat_list(serviceMchntStatList);
			branchServiceMchntStat.setState(Constant.SUCCESS);

		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			branchServiceMchntStat.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			branchServiceMchntStat.setState(Constant.INTER_ERROR);
		}
		return branchServiceMchntStat;
	}

}
